package org.eiahe.hr.employeeRelation.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.ehe.common.core.utils.MapstructUtils;
import org.ehe.system.domain.HrEmployee;
import org.ehe.system.mapper.HrEmployeeMapper;
import org.eiahe.hr.employeeRelation.domain.ContractTerminationApplication;
import org.eiahe.hr.employeeRelation.domain.TechnologyAgreement;
import org.eiahe.hr.employeeRelation.domain.bo.ContractTerminationApplicationBo;
import org.eiahe.hr.employeeRelation.domain.bo.TechnologyAgreementBo;
import org.eiahe.hr.employeeRelation.domain.vo.TechnologyAgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.eiahe.hr.employeeRelation.mapper.TechnologyAgreementMapper;
import org.eiahe.hr.employeeRelation.service.ITechnologyAgreementService;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 技术研发协议服务实现类
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class TechnologyAgreementServiceImpl implements ITechnologyAgreementService {

    private final TechnologyAgreementMapper agreementMapper;
    private final HrEmployeeMapper employeeMapper;
    /**
     * 查询技术研发协议列表
     *
     * @param bo 技术研发协议业务对象
     * @return 技术研发协议列表
     */
    @Override
    public List<TechnologyAgreementVo> selectTechnologyAgreementList(TechnologyAgreementBo bo) {
        LambdaQueryWrapper<TechnologyAgreement> wrapper = Wrappers.lambdaQuery();
        // 根据需要添加查询条件
        return agreementMapper.selectVoList(wrapper);
    }

    /**
     * 查询技术研发协议列表(分页)
     *
     * @param bo        技术研发协议业务对象
     * @param pageQuery 分页参数
     * @return 技术研发协议分页列表
     */
    @Override
    public TableDataInfo<TechnologyAgreementVo> selectTechnologyAgreementList(TechnologyAgreementBo bo,
            PageQuery pageQuery) {
        LambdaQueryWrapper<TechnologyAgreement> lqw = buildQueryWrapper(bo);
        // 根据需要添加查询条件
        IPage<TechnologyAgreementVo> page = agreementMapper.selectVoPage(pageQuery.build(), lqw);
        page.getRecords().forEach(p->{
            Long employeeId = p.getEmployeeId();
            HrEmployee hrEmployee = employeeMapper.selectById(employeeId);
            if(Objects.nonNull(hrEmployee)){
                p.setEmployeeName(hrEmployee.getEmployeeName());
            }
        });
        return TableDataInfo.build(page);
    }
    private LambdaQueryWrapper<TechnologyAgreement> buildQueryWrapper(TechnologyAgreementBo bo) {
        LambdaQueryWrapper<TechnologyAgreement> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(bo.getEmployeeName())) {
            List<HrEmployee> employees = employeeMapper.selectList(
                Wrappers.lambdaQuery(HrEmployee.class)
                    .like(HrEmployee::getEmployeeName, bo.getEmployeeName()));
            List<String> employeeNos = Optional.ofNullable(employees)
                .orElse(Collections.emptyList())
                .stream()
                .map(HrEmployee::getEmployeeNo)
                .collect(Collectors.toList());

            if (CollUtil.isEmpty(employeeNos)) {
                lqw.eq(TechnologyAgreement::getEmployeeNo, -1);
            } else {
                lqw.in(TechnologyAgreement::getEmployeeNo, employeeNos);
            }
        }
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()), TechnologyAgreement::getEmployeeNo, bo.getEmployeeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getAgreementName()), TechnologyAgreement::getAgreementName,
            bo.getAgreementName());
        return lqw;
    }

    /**
     * 通过协议ID查询技术研发协议信息
     *
     * @param agreementId 协议ID
     * @return 技术研发协议信息
     */
    @Override
    public TechnologyAgreementVo selectTechnologyAgreementById(Long agreementId) {
        return agreementMapper.selectVoById(agreementId);
    }

    /**
     * 新增技术研发协议
     *
     * @param bo 技术研发协议业务对象
     * @return 结果
     */
    @Override
    public Boolean insertTechnologyAgreement(TechnologyAgreementBo bo) {
        TechnologyAgreement agreement = MapstructUtils.convert(bo, TechnologyAgreement.class);
        return agreementMapper.insert(agreement) > 0;
    }

    /**
     * 修改技术研发协议
     *
     * @param bo 技术研发协议业务对象
     * @return 结果
     */
    @Override
    public Boolean updateTechnologyAgreement(TechnologyAgreementBo bo) {
        TechnologyAgreement agreement = MapstructUtils.convert(bo, TechnologyAgreement.class);
        return agreementMapper.updateById(agreement) > 0;
    }

    /**
     * 批量删除技术研发协议
     *
     * @param agreementIds 需要删除的协议ID数组
     * @return 结果
     */
    @Override
    public Boolean deleteTechnologyAgreementByIds(Long[] agreementIds) {
        return agreementMapper.deleteBatchIds(Arrays.asList(agreementIds)) > 0;
    }
}
